package com.villaday.idf.custom.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;

import com.google.gson.Gson;
import com.villaday.idf.R;
import com.villaday.idf.adapter3.HouseManageAdapter;

import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.model3.HashInfosEntity;
import com.villaday.idf.model3.HashStatesEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.DateUtils;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PopularUtil;
import com.villaday.idf.util.SharedPreferencesUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import razerdp.basepopup.BasePopupWindow;

public class CalendarPop extends BasePopupWindow {

    private Context mContext;
    private Activity mActivity;
    private final RecyclerView mRvCalendar;

    private HouseManageAdapter mHouseManageAdapter;
    private TextView mTvManage;
    private List<HashStatesEntity.BodyBean.MonthsBean> mMonths;
    private String mHouseId;
    private LoadingDialog mLoadDialog;
    private HashInfosEntity.BodyBean mBody;
    private int mType;


    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popular_calendar);
    }

    public CalendarPop(Context context, String houseId, final String name, String data, int type) {
        super(context);
        mContext = context;
        mActivity = (Activity) context;
        setBackground(0);
        setOutSideDismiss(true);
        setKeepSize(true);
        setPopupFadeEnable(false);
        setBackgroundColor(Color.parseColor("#B3000000"));
        setBlurBackgroundEnable(true);
//        findViewById(R.id.ll_calendar).startAnimation(PopularUtil.buildAnimal());
        mType = type;
        mHouseId = houseId;
        mRvCalendar = findViewById(R.id.rv_calendar);
        mTvManage = findViewById(R.id.tv_c_manage);
        TextView mTvTime = findViewById(R.id.tv_calendar_time);
        mTvTime.setText("更新时间：" + DateUtils.long2Data(System.currentTimeMillis()));
        mTvManage.setEnabled(false);
        mTvManage.setBackgroundResource(R.drawable.shape_gradient_7f_8e_40);

        initRecycle();
        getDate();


        findViewById(R.id.iv_calendar_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mBody = new Gson().fromJson(data, HashInfosEntity.BodyBean.class);
        mTvManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashStatesEntity.BodyBean bodyBean = new HashStatesEntity.BodyBean();
                bodyBean.setMonths(mMonths);
                if (mType == 1) {
                    HousePricePop housePricePop = new HousePricePop(mContext, mActivity, mHouseId, name, new Gson().toJson(mBody), new Gson().toJson(bodyBean));
                    housePricePop.setOnBtnClickListener(new HousePricePop.OnBtnClickListener() {
                        @Override
                        public void onBtnClick(String money) {
                            getDate();
                        }
                    });
                    housePricePop.showPopupWindow();
                } else {
                    DataPop dataPopAdd = new DataPop(mContext, mActivity, mHouseId, name, mBody.getSettlementMode(), "特殊", new Gson().toJson(bodyBean));
                    dataPopAdd.setOnBtnClickListener(new DataPop.OnBtnClickListener() {
                        @Override
                        public void onBtnClick(String title) {
                            showDialog();
                            getDate();
                            dismiss();
                            onBtnClickListener.onBtnClick("完成了");
                        }
                    });
                    dataPopAdd.showPopupWindow();
                }
            }
        });


    }


    private void getDate() {
//        showDialog();
        Map<String, String> map = new HashMap<>();
        map.put("month", DateUtils.getStringMouth(System.currentTimeMillis()));
        OkHttpUtil.getJSON(mContext, String.format(Constant.URL.hashStates, SharedPreferencesUtil.getRoleInfoId(mContext), mHouseId), map, new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {
                try {
                    LogUtil.jLog().e(json);
                    dismissDialog();
                    if (!TextUtils.isEmpty(json)) {
                        HashStatesEntity hashStatesEntity = new Gson().fromJson(json, HashStatesEntity.class);
                        if (hashStatesEntity.getCode().equals(Constant.Strings.SUC) && hashStatesEntity.getBody() != null) {
                            mRvCalendar.setVisibility(View.VISIBLE);
                            findViewById(R.id.tv_calendar_not).setVisibility(View.GONE);
                            HashStatesEntity.BodyBean body = hashStatesEntity.getBody();
                            mMonths = body.getMonths();
                            mHouseManageAdapter.setData(mMonths);
                            mTvManage.setEnabled(false);
                            mTvManage.setBackgroundResource(R.drawable.shape_gradient_7f_8e_40);
                        } else {
                            findViewById(R.id.tv_calendar_not).setVisibility(View.VISIBLE);
                            mRvCalendar.setVisibility(View.GONE);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(String url, String error) {
                dismissDialog();
            }
        });
    }

    private void initRecycle() {
        mRvCalendar.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mHouseManageAdapter = new HouseManageAdapter(mContext);
        mHouseManageAdapter.setOnRecycleItemClickListener(new HouseManageAdapter.OnRecycleItemClickListener() {
            @Override
            public void onItemClick(int position, int itemPosition) {
                boolean isCheck = false;
                for (int i = 0; i < mMonths.size(); i++) {
                    for (int j = 0; j < mMonths.get(i).getDays().size(); j++) {
                        if (i == position && j == itemPosition && mMonths.get(i).getDays().get(j).getState() != 4) {
                            mMonths.get(i).getDays().get(j).setSelect(!mMonths.get(i).getDays().get(j).isSelect());
                        }
                        if (mMonths.get(i).getDays().get(j).isSelect()) {
                            isCheck = true;
                        }
                    }
                }
                mHouseManageAdapter.notifyDataSetChanged();
                mTvManage.setEnabled(isCheck);
                if (isCheck) {
                    mTvManage.setBackgroundResource(R.drawable.shape_fe_ff40);
                } else {
                    mTvManage.setBackgroundResource(R.drawable.shape_gradient_7f_8e_40);
                }
            }
        });
        mRvCalendar.setAdapter(mHouseManageAdapter);
    }

    /**
     * 设置加载框
     */
    protected void showDialog() {
        if (mLoadDialog == null) {
            mLoadDialog = LoadingDialog.newInstance("加载中");
        }
        mLoadDialog.show(mActivity.getFragmentManager(), "loading");
    }


    /**
     * 设置加载框
     */
    protected void dismissDialog() {
        if (mLoadDialog != null) {
            mLoadDialog.dismiss();
        }
    }

//    @Override
//    protected Animation onCreateShowAnimation() {
//        return PopularUtil.ShowAnimation();
//    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return PopularUtil.dismissAnimation();
    }

    public OnBtnClickListener onBtnClickListener;
    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onBtnClick(String title);
    }

}
